{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 活动热度数据（event_attendees.csv）处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# event_attendees.csv文件，共5维特征，内容是以空格隔开的用户列表：\n",
    "# event_id：活动ID\n",
    "# yes：参加该活动的用户\n",
    "# maybe：可能参加的用户\n",
    "# invited：被邀请的用户\n",
    "# no：不参加的用户 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入工具包\n",
    "\n",
    "import pandas as pd\n",
    "\n",
    "import numpy as np\n",
    "import scipy.sparse as ss\n",
    "import scipy.io as sio\n",
    "\n",
    "#保存数据\n",
    "import pickle\n",
    "\n",
    "from sklearn.preprocessing import normalize"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "总的用户数目超过训练集和测试集中的用户， 为节省处理时间和内存，先去处理train和test，得到竞赛需要用到的事件和用户 然后对在训练集和测试集中出现过的事件和用户建立新的ID索引 先运行user_event.ipynb, 得到事件列表文件：PE_userIndex.pkl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of events in train & test:13418\n"
     ]
    }
   ],
   "source": [
    "#读取训练集和测试集中出现过的事件列表\n",
    "\n",
    "eventIndex = pickle.load(open(\"PE_eventIndex.pkl\", 'rb'))\n",
    "n_events = len(eventIndex)\n",
    "\n",
    "print(\"Number of events in train & test:%d\" % n_events)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 处理event_attendees.csv\n",
    "# 定义活动活跃度矩阵\n",
    "eventPopularity = ss.dok_matrix((n_events, 1))\n",
    "\n",
    "# 读取数据 \n",
    "f = open(\"/Users/cuiyue/Desktop/AI/第四周/作业/要求/71-57-1-1519640547/event_attendees.csv\", 'rb')\n",
    "\n",
    "# 字段：'event', 'yes', 'maybe', 'invited', 'no'\n",
    "# 去掉标题行\n",
    "f.readline().decode('utf-8').strip().split(',')\n",
    "\n",
    "for line in f:\n",
    "    cols = f.readline().decode('utf-8').strip().split(',')\n",
    "    eventId = str(cols[0])\n",
    "    if eventId in eventIndex: # 如果事件在测试集与训练集中存在\n",
    "        i = eventIndex[eventId] # 则返回活动的索引号\n",
    "        # 热度用参加用户的数量-不参加用户的数量表示\n",
    "        eventPopularity[i, 0] = len(cols[1].split(\" \")) - len(cols[4].split(\" \")) \n",
    "f.close\n",
    "# 对热度矩阵做正则化处理\n",
    "eventPopularity = normalize(eventPopularity, norm=\"l1\",axis=0, copy=False)\n",
    "# 存储活动热度矩阵\n",
    "sio.mmwrite(\"EA_eventPopularity\", eventPopularity)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
